package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.WmMdZone;
import cn.stylefeng.guns.modular.biz.enums.WmMdZoneExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.WmMdZoneMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.WmMdZoneRequest;
import cn.stylefeng.guns.modular.biz.service.WmMdZoneService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;

/**
 * 煤场基础分区代码业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/13 16:37
 */
@Service
public class WmMdZoneServiceImpl extends ServiceImpl<WmMdZoneMapper, WmMdZone> implements WmMdZoneService {

	@Override
    public void add(WmMdZoneRequest wmMdZoneRequest) {
        WmMdZone wmMdZone = new WmMdZone();
        BeanUtil.copyProperties(wmMdZoneRequest, wmMdZone);
        this.save(wmMdZone);
    }

    @Override
    public void del(WmMdZoneRequest wmMdZoneRequest) {
        WmMdZone wmMdZone = this.queryWmMdZone(wmMdZoneRequest);
        this.removeById(wmMdZone.getZoneId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(WmMdZoneRequest wmMdZoneRequest) {
        this.removeByIds(wmMdZoneRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(WmMdZoneRequest wmMdZoneRequest) {
        WmMdZone wmMdZone = this.queryWmMdZone(wmMdZoneRequest);
        BeanUtil.copyProperties(wmMdZoneRequest, wmMdZone);
        this.updateById(wmMdZone);
    }

    @Override
    public WmMdZone detail(WmMdZoneRequest wmMdZoneRequest) {
        return this.queryWmMdZone(wmMdZoneRequest);
    }

    @Override
    public PageResult<WmMdZone> findPage(WmMdZoneRequest wmMdZoneRequest) {
        LambdaQueryWrapper<WmMdZone> wrapper = createWrapper(wmMdZoneRequest);
        Page<WmMdZone> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<WmMdZone> findList(WmMdZoneRequest wmMdZoneRequest) {
        LambdaQueryWrapper<WmMdZone> wrapper = this.createWrapper(wmMdZoneRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private WmMdZone queryWmMdZone(WmMdZoneRequest wmMdZoneRequest) {
        WmMdZone wmMdZone = this.getById(wmMdZoneRequest.getZoneId());
        if (ObjectUtil.isEmpty(wmMdZone)) {
            throw new ServiceException(WmMdZoneExceptionEnum.WM_MD_ZONE_NOT_EXISTED);
        }
        return wmMdZone;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private LambdaQueryWrapper<WmMdZone> createWrapper(WmMdZoneRequest wmMdZoneRequest) {
        LambdaQueryWrapper<WmMdZone> queryWrapper = new LambdaQueryWrapper<>();

        Long zoneId = wmMdZoneRequest.getZoneId();
        queryWrapper.eq(ObjectUtil.isNotNull(zoneId), WmMdZone::getZoneId, zoneId);

        String zoneName = wmMdZoneRequest.getZoneName();
        queryWrapper.like(ObjectUtil.isNotEmpty(zoneName), WmMdZone::getZoneName, zoneName);

        String zoneCode = wmMdZoneRequest.getZoneCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(zoneCode), WmMdZone::getZoneCode, zoneCode);

        String remark = wmMdZoneRequest.getRemark();
        queryWrapper.like(ObjectUtil.isNotEmpty(remark), WmMdZone::getRemark, remark);


        return queryWrapper;
    }

}
